//
//  BHComAddOrShowImageCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/24.
//

import UIKit

class BHComAddOrShowImageCell: UITableViewCell {

    var didImageActBlock:((Int,Bool,BHImageOrVideoModel?) -> Void)?
    var didDeleteImageAtIndexBlock:((Int) -> Void)?
    
    
    fileprivate var images_array: [BHImageOrVideoModel] = []
    fileprivate var currentIndex: Int = 0
    
    @IBOutlet var imgItemArray: [UIButton]!
    
    @IBOutlet var delBtnArray: [UIButton]!
    
    func configImage(imageArray: [BHImageOrVideoModel],index: Int,canAdd: Bool)  {
        self.images_array = imageArray
        self.currentIndex = index
        for btn in self.imgItemArray {
            btn.isHidden = true
        }
        if imageArray.count == 0 {
            let btn = self.imgItemArray[0]
            btn.setBackgroundImage(UIImage.init(named: "bh_com_addImg_placeBack"), for: .normal)
            btn.isHidden = false
            
            for delBtn in self.delBtnArray {
                delBtn.isHidden = true
            }
        }else {
            let startI = index*3;
            
            for (btnIndex, btn) in self.imgItemArray.enumerated() {
                let delBtn = self.delBtnArray[btnIndex]
                
                let total = startI+btnIndex
                if total < imageArray.count {
                    let model = imageArray[total]
                    if model.imageUrl == nil {
                        btn.setBackgroundImage(model.highQualityFormatImage, for: .normal)
                    }else {
                        btn.sd_setBackgroundImage(with: URL.init(string: model.imageUrl ?? ""), for: .normal, completed: nil)
                    }
                    btn.isHidden = false
                    if canAdd == true {
                        delBtn.isHidden = false
                    }else {
                        delBtn.isHidden = true
                    }
                } else if total == imageArray.count {
                    if canAdd == true {
                        btn.isHidden = false
                        btn.setBackgroundImage(UIImage.init(named: "bh_com_addImg_placeBack"), for: .normal)
                        delBtn.isHidden = true
                    }else {
                        btn.isHidden = true
                        delBtn.isHidden = true
                    }
                }else {
                    btn.isHidden = true
                    delBtn.isHidden = true
                }
            }
        }
    }
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
       
    }

    
    
    
    @IBAction func imgDidAct(_ sender: UIButton) {
        let tag = sender.tag-100
        let startI = self.currentIndex*3+tag;
        if self.images_array.count != 0 && startI < self.images_array.count {
            let model = self.images_array[startI]
            self.didImageActBlock?(startI,true,model)
        }else {
            self.didImageActBlock?(startI,false,nil)

        }
    }
    
    @IBAction func delImgAct(_ sender: UIButton) {
        let tag = sender.tag-200
        let startI = self.currentIndex*3+tag;
        self.didDeleteImageAtIndexBlock?(startI)
    }
    
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
